/*
Author: Michael Puzon
Class: COIN 71
Date: March 19, 2010
Description:
Utility classes used for postride.js and viewride.js
Includes functions for adding nodes to page, and sorting methods.
*/

//creates a new element node, appends it to parent.
// if text != "", this also creates a text node with text in it and appends it to element.
//sets the name and id attributes to nameId.
//adapted From page 48 of Crane and Pescarello.
function addNode(parent, element, nameId, text)
{	var childEl = document.createElement(element);
	childEl.setAttribute("id", nameId);
	childEl.setAttribute("name", nameId);	
	if (text != "")
	{	textNode = document.createTextNode(text);
		childEl.appendChild(textNode);
	}
	parent.appendChild(childEl);
	return childEl;
}

//creates an anchor element to parent
//href-contains address of link
//text-text of link
function addLink(parent, href, text)
{	var aEl = document.createElement("a");
	aEl.setAttribute("href", href);
	if (text != "")
	{	textNode = document.createTextNode(text);
		aEl.appendChild(textNode);
	}
	parent.appendChild(aEl);
	return aEl;
}

//compares date string numerically in ascending order
function compareDateStr(a, b) {	
        return a.date - b.date;
}

//compares date string numerically in descending order
function compareDateStr2(a, b) {
        return  b.date - a.date;
}


//compares ride strings in ascending order
function compareRideStr(a, b) {	
    var arating = a.rating.toLowerCase();
	var brating = b.rating.toLowerCase();		
	return (brating < arating) - (arating < brating);
}

//compares ride strings in descending order
function compareRideStr2(a, b) {
	var arating = a.rating.toLowerCase();
	var brating = b.rating.toLowerCase();		
    return (arating < brating) - (brating < arating);
}

//replaces n3wline with a \n. this is used to glue back data
//that is broken up in json
function newLine (str){
	newstr = str.replace(/ n3wline /g, "\n")
	newstr += "\n";
	return newstr;
}